<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>答题记录</span>
      </div>

      <div class="filter-container">
        <el-input v-model="listQuery.name" :placeholder="'请输入用户信息'" clearable style="width: 200px;margin-right: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-input v-model="listQuery.question" :placeholder="'请输入题目'" clearable style="width: 200px;margin-right: 10px;" class="filter-item" @keyup.enter.native="handleFilter" />
        <el-select v-model="listQuery.is_right" style="margin-right: 10px;" placeholder="是否正确">
            <el-option label="全部" value=""></el-option>
            <el-option label="正确" value="1"></el-option>
            <el-option label="跳过" value="2"></el-option>
            <el-option label="部分正确" value="3"></el-option>
            <el-option label="错误" value="0"></el-option>
        </el-select>

        <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
          {{ '搜索' }}
        </el-button>
        <el-button v-waves :disabled="!checkPermission(['question.export'])"  type="success" icon="el-icon-upload2" @click="handleExport">
            {{ '导出' }}
        </el-button>
      </div>

      <el-table
        v-loading="listLoading"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%"
        row-key="id"
        :tree-props="{children: 'children'}"
      >

        <el-table-column width="100" label='编号'>
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="100px" label='用户'  align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.nickname }}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="100px" label='题目' align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.question.title}}</span>
          </template>
        </el-table-column>
         <el-table-column min-width="80" label='回答' align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.answer_number}}</span>
          </template>
        </el-table-column>
        <el-table-column min-width="100px" label='是否正确' align="center"> 
          <template slot-scope="scope">
              <el-button v-if="scope.row.is_right == 1" size="mini" type="success" round>正确</el-button>
              <el-button v-else-if="scope.row.is_right == 2" size="mini" type="success" round>跳过</el-button>
              <el-button v-else-if="scope.row.is_right == 3" size="mini" type="success" round>部分答对</el-button>
              <el-button v-else size="mini" type="danger" round>错误</el-button>
          </template>
        </el-table-column>


        <el-table-column min-width="150px" align="center" :label="'回答时间'">
          <template slot-scope="scope">
            <span>{{ scope.row.create_time | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
          </template>
        </el-table-column>

      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </el-card>
  </div>
</template>

<script>
import {export_excell} from '@/api/export_excell' // 导出
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // Secondary package based on el-pagination
import permission from '@/directive/permission/index.js' // 权限判断指令
import checkPermission from '@/utils/permission' // 权限判断函数
import { record } from '@/api/question'
import { parseTime } from '@/utils'
export default {
  name: 'question_record',
  components: { Pagination },
  directives: { waves, permission },
  filters: {
    parseTime
  },
  data() {
    return {
      list: [],
      total: 0,
      select_ids:[],
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
     handleExport(){
      //导出
           export_excell(process.env.VUE_APP_BASE_API + 'admin/question_export',this.listQuery);
      },
    checkPermission,
    getList() {
      this.listLoading = true
      record(this.listQuery).then(response => {
        this.list = response.data.data
        console.log(this.list);
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleCreate() {
      this.create.dialogVisible = true
    },
    handleEdit(index, row) {
      this.edit.dialogVisible = true
      this.edit.id = row.id
    },
    handleDownload(){
      location.href = 'https://game.vrupup.com/resources/sam_base/answer.csv';
    },
    handleBatch() {
        var that = this;
        if(this.select_ids.length == 0){
            this.$message({
                message: '请选择要删除的信息',
                type: 'warning'
            });
            return false;
        }
        this.$confirm('确认删除选中的用户?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        }).then(() => {
            del(this.select_ids.join(',')).then(res => {
                this.$message({
                    message: '删除成功',
                    type: 'success'
                });
                this.getList();
            }).catch(err => {
                this.$message({
                    message: err.msg,
                    type: 'error'
                });
            })
        }).catch(() => {
            this.$message({
                type: 'info',
                message: '已取消删除'
            });
        });
    },
    handleDelete(index, row) {
      const thiz = this
      this.$confirm('确认要删除吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        del(row.id).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success',
            duration: 1500,
            onClose() {
              thiz.list.splice(index, 1)
            }
          })
        })
      }).catch(() => {

      })
    }
  }
}
</script>

<style scoped>
.pagination-container {
  padding: 5px 2px;
}
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
